package com.irdstudio.tdp.executor.core.plugin.migrate.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 迁移表映射信息类-[表名: 迁移表映射信息(migrate_table_mapping)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2020-11-04
 */
public class MigrateTableMappingDao {

	/* 连接对象 */
	Connection conn = null;
	MigrationtabledataUtilDao migrationtabledataUtilDao = null;

	public MigrateTableMappingDao(Connection conn){
		this.conn = conn;
		migrationtabledataUtilDao = new MigrationtabledataUtilDao(conn);
	}

	/**
	 * 根据主键查询单条记录
	 * @param 
	 * @return
	 * @throws SQLException
	 */
	public List<MigrateTableMapping> queryMigrateTableMappingWithAll()
			throws SQLException {
		List<MigrateTableMapping> dcList = new ArrayList<MigrateTableMapping>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		MigrateTableMapping dc = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM migrate_table_mapping");
			rs = ps.executeQuery();
			while (rs.next()) {
				dc = new MigrateTableMapping();
				dc.setTableMappingId(rs.getString("table_mapping_id"));
				dc.setSubsCode(rs.getString("subs_code"));
				dc.setTableMappingWay(rs.getString("table_mapping_way"));
				dc.setSourceTableId(rs.getString("source_table_id"));
				dc.setSourceTableCode(rs.getString("source_table_code"));
				dc.setSourceTableName(rs.getString("source_table_name"));
				dc.setSourceSql(rs.getString("source_sql"));
				dc.setTargetTableId(rs.getString("target_table_id"));
				dc.setTargetTableCode(rs.getString("target_table_code"));
				dc.setTargetTableName(rs.getString("target_table_name"));
				dcList.add(dc);
			}
		} catch (SQLException e) {
			throw new SQLException("queryMigrateTableMappingWithAll is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dcList;
	}

	/**
	 * 根据查询条件查询多条或单条记录
	 * @param szCondition 
	 * @return
	 * @throws SQLException
	 */
	public List<MigrateTableMapping> queryMigrateTableMappingWithCond(String szCondition,String szOrderBy)
			throws SQLException {
		List<MigrateTableMapping> dcList = new ArrayList<MigrateTableMapping>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement("SELECT * FROM migrate_table_mapping "
					+ szCondition + " " + szOrderBy);
			String sql="SELECT * FROM migrate_table_mapping "
					+ szCondition + " " + szOrderBy;
			rs = ps.executeQuery();
			while (rs.next()) {
				MigrateTableMapping dc = new MigrateTableMapping();
				dc.setTableMappingId(rs.getString("table_mapping_id"));
				dc.setSubsCode(rs.getString("subs_code"));
				dc.setTableMappingWay(rs.getString("table_mapping_way"));
				dc.setSourceTableId(rs.getString("source_table_id"));
				dc.setSourceTableCode(rs.getString("source_table_code"));
				dc.setSourceTableName(rs.getString("source_table_name"));
				dc.setSourceSql(rs.getString("source_sql"));
				dc.setTargetTableId(rs.getString("target_table_id"));
				dc.setTargetTableCode(rs.getString("target_table_code"));
				dc.setTargetTableName(rs.getString("target_table_name"));
				dcList.add(dc);
			}
		} catch (SQLException e) {
			throw new SQLException("queryMigrateTableMappingWithCond is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dcList;
	}

	/**
	 * 插入目标数据sql语句
	 * @param tableMapping
	 * @param ziduan
	 * @param crvalue
	 * @param targetList
	 * @return
	 * @throws SQLException
	 */
	public boolean insertTheTargetDataSQLStatement(MigrateTableMapping tableMapping,StringBuffer ziduan,StringBuffer crvalue,List<Map> targetList) throws SQLException {
		String datamigrationsql = "insert into "+tableMapping.getTargetTableCode()
				+ziduan.substring(0,ziduan.length() - 1)
				+")"+" values"+crvalue.substring(0,crvalue.length() - 1)
				+")";
		return migrationtabledataUtilDao.datamigration(datamigrationsql,targetList);

	}


	/**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}